<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02  Document</title>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <h3>
            <input type="checkbox" :checked = "checkedAll" @click="check"> 全选
        </h3>
        <h3>
            兴趣爱好
            <span v-for="item in list" :key="item.id">
                <input type="checkbox" :value="item.value" v-model="item.checked" /> {{item.msg}}
            </span>

        </h3>
        
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            checkedAll:false,
            list: [{ id: 1, value: 1, msg: "篮球", checked: false },
            { id: 2, value: 2, msg: "足球", checked: false },
            { id: 3, value: 3, msg: "排球", checked: false }]
        },
        methods:{

            check(){//全选的状态
                let flag = window.event.target.checked;

                this.list.forEach(item=>{
                    item.checked = flag;
                })
            },
        },
        watch:{
           list:{//深度监听
               deep:true,
               handler(newList){
                this.checkedAll = newList.every(item=>item.checked);
               }
           }
        }
    })

</script>

</html>